export default {
	hexCharCodeToStr(hexCharCodeStr) {
		var trimedStr = hexCharCodeStr.trim();
		var rawStr = trimedStr.substr(0, 2).toLowerCase() === "0x" ? trimedStr.substr(2) : trimedStr;
		var len = rawStr.length;
		if (len % 2 !== 0) {
			alert("存在非法字符!");
			return "";
		}
		var curCharCode;
		var resultStr = [];
		for (var i = 0; i < len; i = i + 2) {
			curCharCode = parseInt(rawStr.substr(i, 2), 16);
			resultStr.push(String.fromCharCode(curCharCode));
		}
		resultStr = resultStr.reverse()
		let str = resultStr.join("").replace('=', '')
		let intS = parseFloat(str)
		return intS;
	},


	// ArrayBuffer转16进度字符串示例
	ab2hex(buffer) {
		var hexArr = Array.prototype.map.call(
			new Uint8Array(buffer),
			function(bit) {
				return ('00' + bit.toString(16)).slice(-2);
			}
		)
		var strBuffer = this.hexCharCodeToStr(hexArr.join(''));
		return strBuffer;
		// return hexArr.join('');
	},


	hexToStr(hex, encoding) {
		var trimedStr = hex.trim();
		var rawStr = trimedStr.substr(0, 2).toLowerCase() === "0x" ? trimedStr.substr(2) : trimedStr;
		var len = rawStr.length;
		if (len % 2 !== 0) {
			alert("Illegal Format ASCII Code!");
			return "";
		}
		var curCharCode;
		var resultStr = [];
		for (var i = 0; i < len; i = i + 2) {
			curCharCode = parseInt(rawStr.substr(i, 2), 16);
			resultStr.push(curCharCode);
		}
		// encoding为空时默认为utf-8
		var bytesView = new Uint8Array(resultStr);
		var str = new TextDecoder(encoding).decode(bytesView);
		return str;
	},

	// ArrayBuffer转为字符串，参数为ArrayBuffer对象
	ab2str(buf) {
		return String.fromCharCode.apply(null, new Uint16Array(buf));
	},

	// 字符串转为ArrayBuffer对象，参数为字符串
	str2ab(str) {
		if (!str) {
			return new ArrayBuffer(0);
		}
		var buffer = new ArrayBuffer(str.length);
		// let dataView = new DataView(buffer);
		var bufView = new Uint8Array(buffer);
		for (var i = 0, strLen = str.length; i < strLen; i++) {
			bufView[i] = str.charCodeAt(i);
		}
		return bufView;
	},


	reverseString(str) {
		return str.split('').reverse().join('');
	},

	dataConversion(str) {
		let that = this
		if (!str) {
			return
		}
		let start = str.substring(0, 3);
		let end = str.substring(str.length - 2, str.length);
		if ('JOY' == start && 'DA' == end) {
			that.equipmentDataTran(str);
		}
	},



}